﻿<template>
  <div class="app-container">
    <div class="chart-title">档案管理</div>
    <div class="chart-content">
      <div class="filter-title">
        <div></div>
        <div class="filter-right">
          <el-button type="primary"
                     @click="handleSelect">筛选</el-button>
          <el-button type="primary">导出</el-button>
        </div>
      </div>
      <div class="chart-list">
        <el-table :key="tableKey"
                  v-loading="listLoading"
                  :data="list"
                  element-loading-text="拼命加载中"
                  element-loading-spinner="el-icon-loading"
                  element-loading-background="rgba(255,255,255,0.2)"
                  border
                  fit
                  highlight-current-row
                  style="width: 100%min-height:1000px">
          <el-table-column fixed="left"
                           align="center"
                           :label="$t('table.id')"
                           width="65">
            <template slot-scope="scope">
              <span>{{
                (listQuery.page - 1) * listQuery.limit + scope.$index + 1
              }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('region.districtsName')">
            <template slot-scope="scope">
              <span>{{ scope.row.admin_region_name }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('public.itoc')">
            <template slot-scope="scope">
              <span>{{ scope.row.itoc_name }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('public.circle')">
            <template slot-scope="scope">
              <span>{{ scope.row.merchant_circle_name }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('public.category')">
            <template slot-scope="scope">
              <span>{{ scope.row.category_name }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('public.number')">
            <template slot-scope="scope">
              <span>{{ scope.row.count }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           :label="$t('table.actions')"
                           class-name="small-padding fixed-width"
                           fixed="right">
            <template slot-scope="scope">
              <div class="read-img"
                   @click="handleRead(scope.row)">
                <img src="@/assets/images/read@2x.png" />
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-container">
        <el-pagination background
                       :current-page="listQuery.page"
                       :page-sizes="[10, 20, 30, 50]"
                       :page-size="listQuery.limit"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="total"
                       @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"></el-pagination>
      </div>
      <!-- 编辑 -->
      <el-dialog :title="textMap[dialogStatus]"
                 :visible.sync="dialogFormVisible">
        <el-form ref="dataForm"
                 label-position="right"
                 label-width="30%"
                 style="width: 400px margin-left:120px"
                 size="small">
          <div class="pops">
            <div class="pops-normal">
              <el-form-item :label="$t('operations.region') + ' :'">
                <el-checkbox v-model="checkAll"
                             @change="(data)=>{return handleCheckAllChange(data, 'districtsOptions', 'admin_region_id')}">不限</el-checkbox>
                <el-checkbox-group v-model="listQuery.admin_region_id"
                                   @change="(data)=>{return handleCheckedCitiesChange(data, 'districtsOptions', 'checkAll')}">
                  <el-checkbox v-for="(items, index) in districtsOptions"
                               :label="items.id"
                               :key="index">{{items.name}}</el-checkbox>
                </el-checkbox-group>
              </el-form-item>
              <el-form-item :label="$t('operations.name_2') + ' :'">
                <drop-down-select :data="itocOptions"
                                  :count="itocCount"
                                  :page-size="pageSize"
                                  :id="listQuery.itoc_id"
                                  :name="listQuery.itoc_name"
                                  @getList="(pageNumber, search)=>{return getItoc(pageNumber, search,'getItoc')}"
                                  @addList="(pageNumber, search)=>{return getItoc(pageNumber, search,'addItoc')}"
                                  @choose="(data)=>{return choose(data, 'itoc')}"
                                  @remove="(data)=>{return remove(data, 'itoc')}"></drop-down-select>
              </el-form-item>
              <el-form-item :label="$t('public.circle') + ' :'">
                <el-checkbox v-model="circleCheckAll"
                             @change="(data)=>{return handleCheckAllChange(data, 'circleOptions', 'merchant_circle_id')}">不限</el-checkbox>
                <el-checkbox-group v-model="listQuery.merchant_circle_id"
                                   @change="(data)=>{return handleCheckedCitiesChange(data, 'circleOptions', 'circleCheckAll')}">
                  <el-checkbox v-for="(items, index) in circleOptions"
                               :label="items.id"
                               :key="index">{{items.name}}</el-checkbox>
                </el-checkbox-group>
              </el-form-item>
              <el-form-item :label="$t('terminal.type') + ' :'">
                <el-checkbox v-model="equipmentCheckAll"
                             @change="(data)=>{return handleCheckAllChange(data, 'equipmentOptions', 'category_id')}">不限</el-checkbox>
                <el-checkbox-group v-model="listQuery.category_id"
                                   @change="(data)=>{return handleCheckedCitiesChange(data, 'equipmentOptions', 'equipmentCheckAll')}">
                  <el-checkbox v-for="(items, index) in equipmentOptions"
                               :label="items.id"
                               :key="index">{{items.name}}</el-checkbox>
                </el-checkbox-group>
              </el-form-item>
            </div>
          </div>
        </el-form>
        <div slot="footer"
             class="dialog-footer">
          <div></div>
          <div>
            <el-button @click="dialogFormVisible = false">{{
              $t("table.cancel")
            }}</el-button>
            <el-button type="primary"
                       :disabled="filterDisabled"
                       @click="filterData">{{ $t("table.confirm") }}</el-button>
          </div>
        </div>
      </el-dialog>
      <!-- 设备编号显示 -->
      <el-dialog :title="textMap[dialogStatus]"
                 :visible.sync="dialogShowVisible">
        <el-form ref="dataForm"
                 label-position="right"
                 label-width="35%"
                 size="small">
          <el-form-item :label="$t('terminal.code') + ' :'">
            <div v-for="(items, index) in temp"
                 :key="index">{{ items.code }}</div>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script type="text/javascript">
import dropDownSelect from "@/views/select/dropDownSelect"; // 分页选择控件
export default {
  name: "Itoc",
  components: {
    "drop-down-select": dropDownSelect
  },
  data() {
    return {
      showSearchItem: false,
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      checkAll: false,
      circleCheckAll: false,
      equipmentCheckAll: false,
      showDateStyle: 1,
      listQuery: {
        page: 1,
        limit: 20,
        date: 1,
        category_id: [],
        merchant_circle_id: [],
        itoc_id: undefined,
        admin_region_id: []
      },
      temp: [],
      districtsOptions: [],
      itocOptions: [],
      itocCount: undefined,
      circleOptions: [],
      equipmentOptions: [],
      dialogFormVisible: false,
      dialogShowVisible: false,
      dialogStatus: "",
      textMap: {
        select: "筛选"
      },
      filterDisabled: false,
      pageSize: 20
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    getList() {
      this.listLoading = true;
      const data = {
        rows: this.listQuery.limit,
        page: this.listQuery.page,
        category_id: this.listQuery.category_id.length > 0 ? this.listQuery.category_id.join(",") : "",
        merchant_circle_id: this.listQuery.merchant_circle_id.length > 0 ? this.listQuery.merchant_circle_id.join(",") : "",
        itoc_id: this.listQuery.itoc_id ? this.listQuery.itoc_id : "",
        admin_region_id: this.listQuery.admin_region_id.length > 0 ? this.listQuery.admin_region_id.join(",") : ""
      };
      const path = {
        app: "equipment",
        api: "api_equipment_index_numberStat",
        version: "1.0",
        type: 2,
        data: data
      };
      console.log("JSON.stringify(path)", JSON.stringify(path));
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    handleRead(row) {
      console.log("data", row);
      const path = {
        app: "equipment",
        api: "api_equipment_index_numberStatDetails",
        version: "1.0",
        type: 2,
        data: {
          itoc_id: row.itoc_id,
          merchant_circle_id: row.merchant_circle_id,
          category_id: row.category_id
        }
      };
      console.log("JSON.stringify(path)", JSON.stringify(path));
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    handleSelect() {
      this.dialogStatus = "select";
      this.dialogFormVisible = true;
      // 获取七大区列表
      this.getDistricts();
      this.getBusinessDistrict();
      this.getEquipmentType();
    },
    // 筛选终端销售统计
    filterData() {
      if (!this.filterDisabled) {
        this.filterDisabled = true;
        this.getList();
        this.dialogFormVisible = false;
        this.showDateStyle = this.listQuery.date;
        setTimeout(() => {
          this.filterDisabled = false;
        }, 1000);
      }
    },
    // 多选
    handleCheckedCitiesChange(value, options, all) {
      let checkedCount = value.length;
      this[all] = checkedCount === this[options].length;
    },
    // 全选
    handleCheckAllChange(val, options, name) {
      let all = [];
      if (val) {
        const officers = this[options];
        all = officers.map(function(officer) {
          return officer.id;
        });
      }
      this.listQuery[name] = all;
    },
    // 运营中心选择
    getItoc(pageNumber, search, name) {
      const path = {
        app: "archives",
        api: "api_archives_itoc_itocList",
        version: "1.0",
        data: {
          rows: this.pageSize,
          page: pageNumber || 1,
          name: search || "",
          returnType: name || ""
        }
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    // 获取七大区列表
    getDistricts() {
      const path = {
        app: "archives",
        api: "api_archives_itoc_sevenRegional",
        version: "1.0",
        data: {}
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    // 获取商圈列表
    getBusinessDistrict() {
      const path = {
        app: "archives",
        api: "api_archives_merchantCircle_getList",
        version: "1.0",
        data: {
        }
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    // 设备类型列表
    getEquipmentType() {
      const path = {
        app: "archives",
        api: "api_archives_category_getList",
        version: "1.0",
        data: {
          type: 2
        }
      }
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    choose(data, name) {
      if (name === "itoc") {
        this.listQuery.itoc_id = data.id;
        this.listQuery.itoc_name = data.name;
      }
    },
    remove(data, name) {
      if (name === "itoc") {
        this.listQuery.itoc_id = undefined;
        this.listQuery.itoc_namee = "";
      }
    },
    socketData(res) {
      if (res !== '{"type":"ping"}') {
        const resj = JSON.parse(res);
        if (resj.code === -1) {
          this.$message.error(resj.message);
        } else {
          if (resj.api === "api_wxadmin_admin_keepLogin") {
            this.$store.dispatch("loginTransition", true);
            this.socketApi.sendCache(res => {
              this.socketData(res);
            })
          } else if (resj.api === "api_equipment_index_numberStat") {
            this.total = resj.count;
            this.list = resj.data;
            console.log("this.list", this.list);
            this.listLoading = false;
          } else if (resj.api === "api_equipment_index_numberStatDetails") {
            console.log("resj", resj);
            this.temp = resj.data;
            this.dialogShowVisible = true;
          } else if (resj.api === "api_archives_itoc_sevenRegional") {
            this.districtsOptions = resj.data;
          } else if (resj.api === "api_archives_itoc_itocList" && resj.type === "getItoc") {
            this.itocOptions = resj.data;
            this.itocCount = resj.count;
          } else if (resj.api === "api_archives_itoc_itocList" && resj.type === "addItoc") {
            this.itocOptions = this.itocOptions.concat(resj.data);
          } else if (resj.api === "api_archives_merchantCircle_getList") {
            this.circleOptions = resj.data;
          } else if (resj.api === "api_archives_category_getList") {
            this.equipmentOptions = resj.data;
          }
        }
      }
    },
    // 显示时间格式
    showTitle() {
      switch (this.showDateStyle) {
        case 1:
          return "年（月份）";
        case 3:
          return "年（季度）";
        case 4:
          return "年";
        default:
          break;
      }
    }
  }
};
</script>
